<?php
class Frontend_Controller extends CI_Controller {
    Public $frontend_data, $cache_limit, $enable_cache, $news_per_page, $n_relate_news_per_page;
    Public function __construct() {
        parent::__construct();

        $this->load->library('Mobile_Detect');
        if (file_exists(APPPATH . '../public/cache/') && is_dir(APPPATH . '../public/cache/'))
            $this->config->set_item('cache_path', '../public/cache/');
        $this->frontend_data = new stdClass();
        $this->frontend_data->active_menu = "";
        $this->cache_limit = 60 * 60 * 24 * 30; //thời gian cache
        $this->enable_cache = false; //
        $this->news_per_page = 6; //số tin tức trên mỗi trang
        $this->n_relate_news_per_page = 6; //số tin tức trên mỗi trang
        $this->load->driver('cache');
        $this->load->library('pagination');
        $this->frontend_data->mobile_detect = new Mobile_Detect();
        //Load cache if cache is html and cache exits 
        $this->cache_html_name = 'html_' . uri_string() . '.cache';
        if ($this->enable_cache) {
            $cache_html = $this->cache->file->get($this->cache_html_name);
            if (!empty($cache_html)) {
                echo $cache_html;
                die;
            }
        }
        
        //Load module and model
        $this->load->module('frontend_module');
        $this->frontend_data->product_types = $this->frontend_module->getProductType();
        //Load config and cache db default
        $this->load_config();
        
    }

    Private function load_config() {
        //Load config and push to hash
        $cache_data = null;
        $cache_name = 'frontend_config.data';
        if ($this->enable_cache)
            $cache_data = $this->cache->file->get($cache_name);

        if (empty($cache_data)) {
            $this->frontend_data->connect[] = 'config';
            $tmp = $this->frontend_module->get_configs();
            if (!empty($tmp)) {
                foreach ($tmp as $item) {
                    $cache_data[$item->Name] = $item;
                }
                if ($this->enable_cache)
                    $this->cache->file->save($cache_name, $cache_data, $this->cache_limit);
            }
        }
        $this->frontend_data->config = $cache_data;
    }

    
    Protected function notice_message($msg) {
        $this->frontend_data->message = $msg;
        message($msg);
    }

}

?>